<?php
namespace App\Http\Controllers;

use App\Events\InstallApp;
use App\Http\Requests\InstallRequest;
use App\Services\InstallService;
use Illuminate\Http\Request;

/**
 * 安装程序
 * Class InstallController
 * @package App\Http\Controllers
 */
class InstallController extends Controller
{
    protected $installService;

    public function __construct(InstallService $installService)
    {
        $this->installService = $installService;
    }

    public function index(Request $request)
    {
        $this->checkInstall($this->installService);
        // 当前安装步骤
        session(["install" => ['step' => 0, 'error' => false]]);
        return view('install.index');
    }

    public function step1(Request $request)
    {
        $this->checkInstall($this->installService);
        $step = session("install.step");
        if ($step < 0 || $step > 2) {
            // 跳转到安装首页
            return response()->redirectTo(route('install.index'));
        }

        session(["install.step" => 1]);
        session(["install.error" => false]);

        // 检查文件同时获取相应数据
        $env = $this->installService->checkEnv();  // 检查环境
        $function = $this->installService->checkFunction();  // 检查方法
        $dirAndFile = $this->installService->checkDirAndFile();   // 检查文件夹和文件

        return view('install.step1', compact('env', 'function', 'dirAndFile'));
    }

    public function step2(Request $request)
    {
        $this->checkInstall($this->installService);
        // 获取步骤
        $step = session("install.step");
        if ($step < 1 || $step > 3) {
            return response()->redirectTo(route('install.index'));
        }

        // 修改步骤信息
        session(["install.step" => 2]);
        session(["install.error" => false]);

        return view('install.step2');
    }

    // 步骤二: 保存用户提交的安装信息
    public function storeStep2(InstallRequest $request)
    {
        $this->checkInstall($this->installService);
        // 站点信息
        $data['site_name'] = $request->input('site_name');
        $data['admin'] = $request->input('admin');
        $data['password'] = $request->input('password');
        $data['demo_data'] = boolval($request->input('demo_data'));  // 是否使用测试数据

        // 把站点信息保存到session
        try {
            $this->installService->writeEnv($request->all());  // 把安装基本信息写入到.env文件中
        } catch (\RuntimeException $e) {
            return back()->with('error', '.env文件和.env.example文件不存在!');
        } catch (\Exception $e) {
            return back()->with('error', '未知异常!');
        }

        // 测试数据库连接
        if (!$this->installService->checkSqlConnection()) {
            return back()->with('error', '数据库连接错误!');  // 如果连接失败
        }

        session(["install.access" => true]);  // access: true 表示所有数据准备成功
        session(["install.admin_info" => $data]);  // admin_info: 网站基本配置信息

        return redirect(route('install.step3'));  // 跳转到步骤三
    }

    // 步骤三: 正式开始安装项目
    public function step3()
    {
        $this->checkInstall($this->installService);

        $step = session("install.step");
        $access = session("install.access");

        if (!$access || $step != 2) {
            return response()->redirectTo(route('install.index'));
        }
        session(["install.step" => 3]);
        session(["install.error" => false]);

//        try {
            event(new InstallApp()); // 调用安装应用事件
//        } catch (\PDOException $e) {
//            session(["install.step" => 2]);
//            session(["install.error" => true]);
//            return redirect(route("install.step2"))->with('error', '数据库连接异常, 详细错误信息:' . $e->getMessage());
//        } catch (\Exception $e) {
//            session(["install.step" => 2]);
//            session(["install.error" => true]);
//            return redirect(route("install.step2"))->with('error', '安装过程中未知错误! 详细错误信息:' . $e->getMessage());
//        }

        return view('install.step3');
    }

    // 检查是否安装成功
    private function checkInstall()
    {
        if ($this->installService->isInstall()) {
            return redirect("admin/auth/login");
        }
    }
}
